<template>
    <van-button
      :class="{
        collected:value
      }"
      :icon="value?'star':'star-o'"
      :loading='loading'
      @click="onCollectArticle"
    ></van-button>
</template>

<script>
// 收藏取消收藏接口
import { addCollect, deleteCollect } from '@/api/articles'
export default {
  name: 'CollectArticle',
  components: {},
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  created () {

  },
  mounted () {

  },
  methods: {
    async onCollectArticle () {
      this.loading = true
      try {
        // 判断是收藏还是取消收藏
        if (this.value) {
          const { data } = await deleteCollect(this.articleId)
          console.log(data)
          this.$toast.success('取消收藏成功')
        } else {
          const { data } = await addCollect(this.articleId)
          console.log(data)
          this.$toast.success('收藏成功')
        }
        this.$emit('input', !this.value)
      } catch (err) {
        this.$toast.fail('收藏失败，请稍后再试')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less">
.collected {
  color: #ffa500;
}
</style>
